package com.example.hyw.szysteam.view;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.example.hyw.szysteam.POJO.UserInfo;
import com.example.hyw.szysteam.R;
import com.example.hyw.szysteam.presenter.JoinClassPresenter;
import com.example.hyw.szysteam.utils.ShowTips;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;

/**
 * Created by HYW on 2018/6/8.
 */

public class JoinClassDialog extends DialogFragment {
    View view;
    TextView inviteCode;
    QMUIRoundButton join;
    private JoinClassPresenter joinClassPresenter=new JoinClassPresenter(this);

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_joinclass,container,false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    public void initView(){
        inviteCode=view.findViewById(R.id.input_invitecode);
        join=view.findViewById(R.id.button_joinclass);
        join.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(inviteCode.getText().toString()!=""){
                    joinClassPresenter.joinClass(UserInfo.getEmail(),inviteCode.getText().toString());
                }else {
                    ShowTips.show(QMUITipDialog.Builder.ICON_TYPE_INFO,"请输入邀请码",join,getContext());
                }
            }
        });
    }

    public void joinSuccess(){
        ShowTips.show(QMUITipDialog.Builder.ICON_TYPE_SUCCESS,"加入成功",join,getContext());
        this.dismiss();
    }

    public void joinFail(int i){
        if (i==1){
            ShowTips.show(QMUITipDialog.Builder.ICON_TYPE_FAIL,"班级不存在",join,getContext());
        }else if(i==2){
            ShowTips.show(QMUITipDialog.Builder.ICON_TYPE_FAIL,"您已在班级中",join,getContext());
        }
    }
}
